package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.RoleDao;
import com.ktjy.ktmall.entity.Role;
import com.ktjy.ktmall.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RoleDaoImpl extends BaseDao implements RoleDao {
    @Override
    public int insertRole(Role role) {
        String sql = "INSERT INTO t_role(`roleName`,`psIds`,`roleDesc`,`enabled`) values(?,?,?,?)";
        return super.executeUpdate(sql, role.getRoleName(),
                role.getPsIds(),
                role.getRoleDesc(),
                role.getEnabled());
    }

    @Override
    public int updateRole(Role role) {
        String sql = "UPDATE t_role set `roleName` = ?,`psIds` = ?,`roleDesc`=?,`enabled`=? where roleId = ? ";
        return super.executeUpdate(sql, role.getRoleName(),
                role.getPsIds(),
                role.getRoleDesc(),
                role.getEnabled(),
                role.getRoleId());
    }

    @Override
    public int deleteRole(Integer id) {
        String sql = "delete from t_role where roleId = ?";
        return super.executeUpdate(sql, id);
    }

    @Override
    public Role selectRoleById(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        Role a = null;
        String sql = "select * from t_role where `roleId` = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                a = new Role(rs.getInt("roleId"),
                        rs.getString("roleName"),
                        rs.getString("psIds"),
                        rs.getString("roleDesc"),
                        rs.getInt("enabled"),
                        rs.getString("create_time"),
                        rs.getString("update_time")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return a;
    }

    @Override
    public List<Role> getRoleListPage(Integer from, Integer pageSize) {
        List<Role> list = new ArrayList<>();
        // 查询已上架的商品分页列表
        String sql = "select * from `t_role` order by create_time desc limit ?,?";
        rs = super.executeQuery(sql, from, pageSize);
        try {
            while (rs.next()) {
                Role a = new Role(rs.getInt("roleId"),
                        rs.getString("roleName"),
                        rs.getString("psIds"),
                        rs.getString("roleDesc"),
                        rs.getInt("enabled"),
                        rs.getString("create_time"),
                        rs.getString("update_time")
                );
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public int count() {
        int count = 0;
        String sql = "select count(*) from `t_role`";
        rs = super.executeQuery(sql);
        try {
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return count;
    }

}
